/**
  ***********************************************************************************
  * @file   : Service_Communication.cpp
  * @brief  : Communication support file.This file provides access ports to interface
  *           with connected devices.
  ***********************************************************************************
                                 ##### Port List #####
  =================================================================================
  |Port Name     Physical-Layer     Data-Link-Layer    Application-Layer    Number
  |EXAMPLE_Port       CAN1               CAN                CUSTOM            0
  |CAN2_Port          CAN2               CAN                Custom            1
  |EBUG_Port         USART1             Custom              Custom            2
  |USART2_Port       USART2              DBUS               DJI-DR16          3
  *
**/
/* Includes ------------------------------------------------------------------*/
#include "Service_Communication.h"
#include "Service_Devices.h"
#include "string.h"
#include "usbd_cdc_if.h"
/* Private define ------------------------------------------------------------*/
void Task_IICTransmit(void *arg);
void Task_IICRecieve(void *arg);
void Task_USBTransmit(void *arg);
void Task_USBRecieve(void *arg);

TaskHandle_t IICTxPort_Handle;
TaskHandle_t IICRxPort_Handle;
TaskHandle_t USBTxPort_Handle;
TaskHandle_t USBRxPort_Handle;

/**
* @brief  Initialization of communication service
* @param  None.
* @return None.
*/
void Service_Communication_Init(void)
{   
  xTaskCreate(Task_IICTransmit,		"IIC Transmit", 	Normal_Stack_Size,    NULL, PriorityNormal,   	&IICTxPort_Handle);
  xTaskCreate(Task_IICRecieve,	 	"IIC Receive" ,   Normal_Stack_Size,    NULL, PriorityAboveNormal,   	&IICRxPort_Handle);
	xTaskCreate(Task_USBTransmit,		"USB Transmit", 	Normal_Stack_Size,    NULL, PriorityAboveNormal,   	&USBTxPort_Handle);
  xTaskCreate(Task_USBRecieve,	 	"USB Receive" ,   Normal_Stack_Size,    NULL, PriorityAboveNormal,   	&USBRxPort_Handle);
}

/*----------------------------------------------- IIC Manager ---------------------------------------------*/

/*Function Prototypes--------------------*/

void Task_IICTransmit(void *arg)
{
  /* Cache for Task */
	uint8_t data[3];
  /* Pre-Load for task */
  data[0] = 1;
	data[1] = 2;
	data[2] = 3;
  /* Infinite loop */
  for(;;)
  {
    /* IIC Send Port */
//		if(HAL_I2C_Master_Transmit(&hi2c1, 0x0B, data, 3, 1000) == HAL_OK){
//			HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
//		}
		vTaskDelay(1000);
		
  }
}

uint16_t buffer[1600];
void Task_IICRecieve(void *arg)
{
  /* Cache for Task */
//  uint8_t free_can_mailbox;
	uint8_t data[3];
  /* Pre-Load for task */
	HAL_I2C_Slave_Receive_IT(&hi2c1, (uint8_t*)buffer, sizeof(buffer));
  /* Infinite loop */
  for(;;)
  {
//		if(HAL_I2C_Slave_Receive(&hi2c1, buffer, sizeof(buffer),1000) == HAL_OK){
////			CDC_Transmit_FS(buffer,sizeof(buffer));
//			HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
//		}
		vTaskDelay(100);
  }
}

void Task_USBTransmit(void *arg)
{
  /* Cache for Task */
	
  /* Pre-Load for task */
//  unsigned char buff[] = {"abcd\n\r"};
  /* Infinite loop */
  for(;;)
  {
			CDC_Transmit_FS((uint8_t*)buffer,sizeof(buffer));
			vTaskDelay(1000);
    /* CAN1 Send Port */
//    if(xQueueReceive(CAN1_TxPort,&CAN_TxMsg,portMAX_DELAY) == pdPASS)
    {

    }
  }
}

void Task_USBRecieve(void *arg)
{
  /* Cache for Task */
//  uint8_t free_can_mailbox;
	
  /* Pre-Load for task */
  
  /* Infinite loop */
  for(;;)
  {
    /* CAN1 Send Port */
//    if(xQueueReceive(CAN2_TxPort,&CAN_TxMsg,portMAX_DELAY) == pdPASS)
    {

    }
		vTaskDelay(1000);
  }
}

void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *I2cHandle)
{
   CDC_Transmit_FS((uint8_t*)buffer,sizeof(buffer));   
   HAL_I2C_Slave_Receive_IT(&hi2c1, (uint8_t*)buffer, sizeof(buffer));
		HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
}


/************************ COPYRIGHT(C) SCUT-ROBOTLAB **************************/
